package com.ryan.encrypttool.business.controller;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import com.ryan.encrypttool.business.auth.message.WebSocketServer;
import org.apache.tomcat.websocket.WsSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ryan.encrypttool.business.auth.message.WebSocketSessionManager;
import org.springframework.web.server.WebSession;
import org.springframework.web.socket.WebSocketSession;

/**
 * @program encrypt-tool
 * @description
 * @author ryan
 * @create 2024-03-19 23:47
 **/
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private WebSocketSessionManager webSocketSessionManager;

    // 推送数据到websocket客户端 接口
    @GetMapping("/socket/push/{cid}")
    public Map pushMessage(@PathVariable("cid") String cid, String message) {
        Map<String, Object> result = new HashMap<>();
        try {
            HashSet<String> sids = new HashSet<>();
            sids.add(cid);
            WebSocketServer.sendMessage("服务端推送消息：" + message, sids);
            result.put("code", cid);
            result.put("msg", message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

}
